﻿//库存管理 - 商品出库
//喻涛林 2008.3
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_LibraryOut : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            initDept();
            getdata();
            initData();
            Label3.Text = DateTime.Now.ToString();
            //txtTime.Attributes.Add("onclick", "SelectDate('txtTime')");
            Label4.Text = DateTime.Now.ToString();
            Hidid.Value = Request.Cookies["user"].Values["id"];
            Hidware.Value = getWareId();
            lblSalesCode.Text = Request.QueryString["SalesCode"];
            HidSaleCode.Value = Request.QueryString["SalesCode"];
            lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblPath.Text = f.getModuleidAdd(Request.QueryString["moduid"]);
            HidModuid.Value = Request.QueryString["moduid"];//栏目编号

        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)//保存数据
    {
        ZSql sel = new ZSql();
        ZSql zsout = new ZSql();
        string WarehouseId;
        for (int i = 0; i < GvData.Rows.Count;i++ )
        {
            WarehouseId =Request["WId"+GvData.Rows[i].Cells[0].Text];
            sel.Open("update CS_Sales set IsOut=1,WarehouseId=" + WarehouseId + " where SalesId=" + GvData.Rows[i].Cells[0].Text);
            sel.Close();

            zsout.Open("insert into CS_OutManage(OutBarCode,guestMan,phone,OutMan,DeliveryMan,OutDate,Content)values('" + HidSaleCode.Value + "','" + Common.FormatParameter(txtname.Text) + "','" + Common.FormatParameter(txtTel.Text) + "','" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "','" + ddlName.SelectedItem.Text + "','" + Label3.Text + "','" + Common.FormatParameter(txtContent.Text) + "')");
            zsout.Close();
            //Response.Write("update CS_Sales set IsOut=1,WarehouseId=" + WarehouseId + " where SalesId=" + GvData.Rows[i].Cells[0].Text);
           //Response.End();
            //ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + HidSaleCode.Value + "')", true);
            Response.Write("<script type='text/javascript'>window.open('DrawPrint.aspx?id=" + HidSaleCode.Value + "','','width=800px,height=650px,left=100px,top=30px,scrollbars=yes');location.href='OutManage.aspx?moduid=" + HidModuid.Value + "';</script>");
        }
    }

    private string getWareId()
    {
        ZSql zs = new ZSql();
        string sql = "select WarehouseId from UserInfo where userid=" + Hidid.Value;
        zs.Open(sql);
        zs.Close();
        return zs.m_table.Rows[0]["WarehouseId"].ToString();
    }

    private string initBarCode()
    {
        ZSql zs = new ZSql();
        string id = "XS" + DateTime.Now.ToString("yyyyMMdd");
        zs.Open("select max(SalesId) as a from CS_Sales");
        if (string.IsNullOrEmpty(zs.m_table.Rows[0]["a"].ToString()))
        {
            id += 1;
        }
        else
        {
            id += Convert.ToInt32(zs.m_table.Rows[0]["a"]) + 1;
        }

        return id;

    }

    private void initDept()
    {

        ZSql dept = new ZSql();
        dept.Open("select deptid,deptname from Department");

        ddlDept.DataSource = dept.m_table;
        ddlDept.DataTextField = "deptname";
        ddlDept.DataValueField = "deptid";
        ddlDept.DataBind();

        ddlDept.Items.Insert(0, new ListItem("—选择部门—", ""));
        ddlName.Items.Insert(0, new ListItem("—选派员工—", ""));
        dept.Close();
    }


    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        ZSql name = new ZSql();
        name.Open("select StaffId,StaffName from CS_Staff where deptid=" + ddlDept.SelectedValue);
        ddlName.DataSource = name.m_table;
        ddlName.DataTextField = "StaffName";
        ddlName.DataValueField = "StaffId";
        ddlName.DataBind();
        ddlName.Items.Insert(0, new ListItem("—选派员工—", ""));
        name.Close();
    }
    protected void getdata()//客户信息
    {
        ZSql getdata = new ZSql();
        if (!string.IsNullOrEmpty(Request["SalesCode"]))
        {
            getdata.Open("select a.guestid,number,c.GuestName,c.Phone,comodBarcode,modul,b.SpecificationsId from CS_Sales a join Specifications b on a.modul=b.Specifications join CS_Guest c on a.guestid=c.GuestId where IsOut=0 and SalesCode='" + Request["SalesCode"] + "'");
            if (getdata.m_table.Rows.Count > 0)
            {
                txtname.Text = getdata.m_table.Rows[0]["GuestName"].ToString();
                txtTel.Text = getdata.m_table.Rows[0]["Phone"].ToString();
            }
        }
    }
    private void initData()//订单数据
    {
        if (!string.IsNullOrEmpty(Request.QueryString["SalesCode"]))
        {
            string sql = "select SalesId,SalesCode,comodid,Convert(char(10),C_date,120) as cdate,typeStock,GuestName,a.WarehouseId,comodName,modul,unit,comodBarcode,prices,number,discount,TotalMoney,realname from CS_Sales a join CS_Guest b on a.guestid=b.guestid join UserInfo c on a.userid=c.userid where a.SalesCode='" + Request.QueryString["SalesCode"] + "'";

            ZSql dept = new ZSql();
            dept.Open(sql);

            this.GvData.DataSource = dept.m_table;
            this.GvData.DataBind();
            dept.Close();
        }

    }
}
